package com.smithlibraties.util.annotation.register.loader;

import com.smithlibraties.SmithLibraries;
import com.smithlibraties.util.annotation.AnnotationClassGetter;
import com.smithlibraties.util.annotation.register.Register;
import com.smithlibraties.util.annotation.register.RegisterClass;
import com.smithlibraties.util.annotation.register.RegisterType;
import com.smithlibraties.util.core.Utils;
import cpw.mods.fml.common.registry.GameRegistry;
import net.minecraft.block.Block;
import net.minecraft.item.Item;
import net.minecraft.item.ItemBlock;
import net.minecraftforge.oredict.OreDictionary;

import java.lang.reflect.Field;
import java.util.ArrayList;
import java.util.List;

public class ItemBlockRegister {
    List<List<RegisterAttribute>> list = new ArrayList<>();//每个类为一个List，然后吧每个类的List集中到这个List

    public ItemBlockRegister() {
        List<Class> itemBlockCollections = AnnotationClassGetter.getAllClassByInterface(SmithLibraries.getAllClasses(),RegisterClass.class);//获取实现RegisterClass注释的类的List
        for (Class clazz : itemBlockCollections) {//开始集中继承IRegister接口的的类的属性的List
            list.add(ItemBlockRegister.getAnnotationList(clazz));
        }
        for (List<RegisterAttribute> fieldList : list) {//遍历这个List的集合
            for (RegisterAttribute registerAttribute : fieldList) {//然后遍历每个类的List
                if (registerAttribute.getOreName() != null) {
                    try {
                        ItemBlockRegister.register(registerAttribute.getType(), registerAttribute.getValue(), registerAttribute.getField().get(null));
                    } catch (IllegalAccessException e) {
                        Utils.mcPrint("物品/方块注册失败！");
                        e.printStackTrace();
                    }
                } else {
                    try {
                        ItemBlockRegister.register(registerAttribute.getType(), registerAttribute.getValue(), registerAttribute.getOreName(), registerAttribute.getField().get(null));
                    } catch (IllegalAccessException e) {
                        Utils.mcPrint("物品/方块（矿物词典）注册失败！");
                        e.printStackTrace();
                    }
                }
            }
        }
    }

    private static void register(RegisterType type, String value, Object object) {
        if (type == RegisterType.ITEM) {
            GameRegistry.registerItem((Item) object, value);
        } else if (type == RegisterType.BLOCK)
            GameRegistry.registerBlock((Block) object, value);
    }

    private static void register(RegisterType type, String value, String ore, Object object) {
        if (type == RegisterType.ITEM) {
            GameRegistry.registerItem((Item) object, value);
            OreDictionary.registerOre(ore, (Item) object);
        } else if (type == RegisterType.BLOCK) {
            GameRegistry.registerBlock((Block) object, value);
            OreDictionary.registerOre(ore, new ItemBlock((Block) object));
        }
    }

    private static List<RegisterAttribute> getAnnotationList(Class clazz) {
        List<RegisterAttribute> list1 = new ArrayList<>();
        for (Field f : clazz.getFields()) {
            f.setAccessible(true);
            if (f.isAnnotationPresent(Register.class)) {
                Register register = f.getAnnotation(Register.class);
                if (f.isAnnotationPresent(Register.OreName.class)) {
                    Register.OreName registerOre = f.getAnnotation(Register.OreName.class);
                    list1.add(new RegisterAttribute(f, register.type(), register.value(), registerOre.value()));
                } else
                    list1.add(new RegisterAttribute(f, register.type(), register.value()));
            }
        }
        return list1;
    }
}
